<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2020/9/22
 * Time: 14:57
 */

namespace App\Inventor\Controllers;


use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Illuminate\Routing\Controller;

class UserController extends Controller
{

    /**
     * User setting page.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function getSetting(Content $content)
    {
        $form = $this->settingForm();
        $form->tools(
            function (Form\Tools $tools) {
                $tools->disableList();
                $tools->disableDelete();
                $tools->disableView();
            }
        );

        return $content
            ->title(trans('admin.user_setting'))
            ->body($form->edit(Admin::user()->id));
    }

    /**
     * Update user setting.
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function putSetting()
    {
        return $this->settingForm()->update(Admin::user()->id);
    }
    /**
     * Model-form for user setting.
     *
     * @return Form
     */
    protected function settingForm()
    {
        $class = config('admin.database.users_model');

        $form = new Form(new $class());
        $form->column(1/2,function(Form $form){
            $form->display('username', trans('admin.username'));
            $form->display('name', trans('姓名'));
            $form->display('department.name',__('Department'));
            $form->display('eno', '工号');
            $form->mobile('mobile','电话')->required();
            $form->password('password', trans('admin.password'))->rules('required|confirmed');
            $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
                ->default(function ($form) {
                    return $form->model()->password;
                });
            $form->ignore(['password_confirmation']);
        });
        $form->column(1/2,function(Form $form){
            $form->text('id_card','身份证号/护照号');
            $form->email('email','电子邮件');
            $form->date('birth','出生日期')->width('120px');
            $form->text('education','学历');
            $form->text('academic_title','职称');
            $form->radio('sex','性别')->options(['1'=>'男',2=>'女'])->default(1);
            //$form->image('avatar', trans('admin.avatar'))->uniqueName();
        });
        //$form->image('avatar', trans('admin.avatar'))->uniqueName();
        $form->setAction(admin_url('auth/setting'));

        $form->ignore(['password_confirmation']);

        $form->saving(function (Form $form) {
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
            }
        });

        $form->saved(function () {
            admin_toastr(trans('admin.update_succeeded'));

            return redirect(admin_url('auth/setting'));
        });

        return $form;
    }

}